<?php
class Specs_mdl extends CI_Model{
  //操作的表.
  const TAB_NAME='clickrcms_specs';
  const TAB_ITEM_NAME='clickrcms_specs_items';
  const TAB_CHECKBOX_NAME='clickrcms_product_itemcheck';
  function __construct(){
      parent::__construct();
     log_message('debug','Specs_mdl Model class Initialized');
  }
  /****
   *查詢所orders,加分页
   *$data :查询参数.
   */
  public function get_specs_all($data=array()){
    if(!empty($data)){
      extract($data);
      isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('add_time DESC');
      isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
	  $name = "name";
      if(isset($language_id) && $language_id == 2){
          $name = "name_en as name";
      }  
      isset($status) && !empty($status)?$this->db->where('status',$status):'';
	  $this->db->select("id,cat_id,$name,sort,status,pos_id,is_pos,add_time");
      return $this->db->get(self::TAB_NAME);
    }
  }
  public function getSpecField($condition=array()){
    $this->db->cache_on();
	if(is_array($condition)){
		$this->db->where($condition);
	}
	$this->db->order_by('sort asc');
	$specs = $this->db->get(self::TAB_NAME)->result_array();
	if($specs){
		$base = array();
		foreach ($specs as $key => $spec) {
			$base[$spec['id']] = $spec;
		}
		return $base;
	}
	return null;
  }
  public function getSpecByIds($ids=array()){
    if(is_array($ids)){
      $this->db->where_in('id',$ids);
    }
    $this->db->order_by('sort asc');
    return $specs = $this->db->get(self::TAB_NAME)->result_array();
  }
  public function getSpecItemField($condition=array(),$is_spce=false){
    if(!$is_spce && is_array($condition)){
         $this->db->where($condition);
    }else{
        $this->db->where_in('spec_id',$condition);
    }

    $this->db->order_by('id asc');

    $items = $this->db->get(self::TAB_ITEM_NAME)->result_array();
    if($items){
      $base = array();
      foreach ($items as $key => $item) {
         $base[$item['id']] = $item;
      }
      return $base;
    }
    return null;
  }
  public function get_spec_items($condition){
  		if(is_array($condition)){
  			$this->db->where($condition);
  			$this->db->order_by('sort asc');
  			return $this->db->get(self::TAB_NAME);
  		}
  		return false;
  }
    /*判断是否存在*/
  public function check_spec_item($id){
      $this->db->where('id',$id);
      $result = $this->db->get(self::TAB_ITEM_NAME)->row_array();
      if($result){
          return true;
      }
      return false;
  }
  public function add($data){
  	  $this->db->insert(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0) ? $this->db->insert_id() :false;
  }
  public function addItem($data){
  	  $this->db->insert_batch(self::TAB_ITEM_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
  }

   /***
   * 修改一個Specs
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function _editItem($data=array(),$id=0){
    if(!empty($data) && !empty($id)){
      $this->db->where('id',$id);
      $this->db->update(self::TAB_ITEM_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 修改一個Specs
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function _edit($data=array(),$id=0){
    if(!empty($data) && !empty($id)){
      $this->db->where('id',$id);
      $this->db->update(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  public function get_spec_byid($id){
  	  if($id>0){
  	  		$this->db->where('id',$id);
  	  		return $this->db->get(self::TAB_NAME)->row_array();
  	  }	
  	  return null;
  }
  public function delete($condition=array()){
  		if(is_array($condition)){
  			$this->db->where($condition);
  			$this->db->delete(self::TAB_NAME);
  			if($this->db->affected_rows()>0){
  				$this->deleteItem(array('spec_id'=>$condition['id']));
  				return true;
  			}
      		return false;
  		}
  		return false;
   }
   public function deleteItem($condition=array()){
   		if(is_array($condition)){
  			$this->db->where($condition);
  			$this->db->delete(self::TAB_ITEM_NAME);
      		return ($this->db->affected_rows()>0)?true:false;
  		}
  		return false;
   }
   public function getItemsBYSpecId($spec_id){
   		if($spec_id > 0){
   			$this->db->where('spec_id',$spec_id);
   			return $this->db->get(self::TAB_ITEM_NAME)->result_array();
   		}
   		return false;
   }
   public function _get_posprint($id=array()){
        if(!empty($id)){
            $this->db->where_in('sp.id', $id);
            $this->db->select('sp.id,sp.name');
            $this->db->from('clickrcms_specs_items AS sp');
            $list   = array();
            $result = $this->db->get();
            if($result->num_rows())
                $list = $result->result_array();
            return $list;
        }
    }
    public function get_items_price($key){
      if(empty($key)){
         return 0;
      }
      $key = explode("_", $key);
      $this->db->where_in('id',$key);
      $result = $this->db->get(self::TAB_ITEM_NAME);
      $total = 0;
      if($result->num_rows()){
          $list = $result->result_array();
          foreach ($list as $key => $item) {
            $total += (float)$item['price'];
          }
      }
      return $total;
  }
  public function _get_itemsBYIds($id=array(),$lang=null){
        if(!empty($id)){
            $this->db->where_in('sp.id', $id);
            $this->db->select('sp.id,sp.name,sp.spec_id,sp.price');
            $this->db->from('clickrcms_specs_items AS sp');
            $list   = array();
            $result = $this->db->get();
            if($result->num_rows())
                $list = $result->result_array();
            return $list;
        }
        return null;
    }
  public function get_items_checkbox($product_id,$lang=null){
     $this->db->where('product_id', $product_id);
     $result = $this->db->get(self::TAB_CHECKBOX_NAME);
     $checkboxs = $result->result_array();
     $data = array();
     if($checkboxs){
        $base = array();
        foreach ($checkboxs as $key => $val) {
            $base[$val['spec_id']][] = $val['item_id'];
        }
        $data = array();
        foreach($base as $spec_id=>$items){
             $specs  = $this->get_spec_byid($spec_id);
             if(empty($specs)){
                continue;
             }
             $data[$spec_id]['name'] = $specs['name'];
             $itemArr = $this->_get_itemsBYIds($items,$lang);
             if(empty($itemArr)){
                continue;
             }
             $data[$spec_id]['items'] = $this->_get_itemsBYIds($items); 
        }
     }
     return $data;
  }
  public function get_items_checkbox_array($product_id,$lang=null){
     $this->db->where('product_id', $product_id);
     $result = $this->db->get(self::TAB_CHECKBOX_NAME);
     $checkboxs = $result->result_array();
     $base = array();
     if($checkboxs){
        $base = array();
        
        foreach ($checkboxs as $key => $val) {
            $base[] = (int)$val['item_id'];
        }
      }
      return $base;
  }
  /*获取产品属性的价格*/
   public function getProductItemUnitPrice($spec_key=null){
      if(!empty($spec_key)){
          $item_ids = explode("_", $spec_key);
          $this->db->where_in('id',$item_ids);
          $items = $this->db->select_sum('price')->get(self::TAB_ITEM_NAME)->row_array();
          return isset($items['price']) ? $items['price'] : 0;
      }

      return 0;
  }
}